<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Notifications\DatabaseNotification;
use Illuminate\Notifications\DatabaseNotificationCollection;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Hash;
use Spatie\Permission\Traits\HasRoles;

/***************前台用户模型******************/
/**
 * App\Models\User
 *
 * @property int $id
 * @property int|null $pid 上级用户id
 * @property string|null $mobile 手机号
 * @property string|null $username
 * @property string|null $nickname 微信昵称
 * @property float|null $total_money 用户总金币
 * @property float|null $current_money 用户当前金币余额
 * @property string|null $openid
 * @property string|null $session_key 回话密钥
 * @property string|null $api_token
 * @property string|null $address 收货地址
 * @property string|null $mp_code 个人二维码地址
 * @property string|null $avatar 微信头像
 * @property string|null $email
 * @property string|null $email_verified_at
 * @property string|null $password
 * @property string|null $remember_token
 * @property string|null $can_hexiao 1：能核销；2：不能核销。
 * @property Carbon|null $created_at
 * @property Carbon|null $updated_at
 * @property-read DatabaseNotificationCollection|DatabaseNotification[] $notifications
 * @property-read int|null $notifications_count
 * @property-read User|null $parent
 * @property-read Collection|Permission[] $permissions
 * @property-read int|null $permissions_count
 * @property-read Collection|Role[] $roles
 * @property-read int|null $roles_count
 * @method static \Illuminate\Database\Eloquent\Builder|User newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|User newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|User permission($permissions)
 * @method static \Illuminate\Database\Eloquent\Builder|User query()
 * @method static \Illuminate\Database\Eloquent\Builder|User role($roles, $guard = null)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereApiToken($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereAvatar($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereCanHexiao($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereCurrentMoney($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereEmail($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereEmailVerifiedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereMobile($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereMpCode($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereNickname($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereOpenid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User wherePassword($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User wherePid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereRememberToken($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereSessionKey($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereTotalMoney($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|User whereUsername($value)
 * @method static where(string $string, mixed $openid)
 * @method static create(array $array)
 * @mixin \Eloquent
 */
class User extends Authenticatable
{
    use Notifiable,HasRoles;

    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'username', 'email', 'password','mobile','remember_token','api_token','mp_code',
        'session_key','openid','total_money','current_money','pid','avatar','nickname'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    //查询当前登陆用户
    public function get_login_user($api_token, $lockForUpdate = false) {
        $query = $this->query();

        $query->where('api_token', $api_token);
        if ($lockForUpdate) {
            $query->lockForUpdate();
        }
        return $query->first();
    }

    public function setPasswordAttribute($value)
    {
        $this->attributes['password'] = Hash::make($value);
    }

    //好友数量
    public function friends_num($uid) {
        //SELECT COUNT(1) FROM users WHERE pid in (SELECT id FROM users WHERE pid=109);
        //先查询出所有一级好友
        $one = self::select('id')->where('pid',$uid)->get()->toArray();
        $one_num = count($one);
        //二级好友数量
        $two_num = self::whereIn('pid',$one)->count();
        return $one_num + $two_num;
    }

    //上级用户
    public function parent() {
        return $this->hasOne('App\Models\User','id','pid');
    }
    //上上级用户
    public function parent_parent() {
        $parent = $this->parent();
        return $parent->hasOne('App\Models\User','id','pid');
    }

    //查询用户,默认分页
    function get_users($where,$limit = '',$paginate = true) {
        if ($paginate) {
            return self::where($where)->paginate($limit);
        }else {
            return self::where($where)->get();
        }
    }

    //流水


}
